#include <iostream>
#include <vector>

using namespace std;

static vector<int> prime_factorization(int num) {
    vector<int> factors;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) 
            factors.push_back(i);
        while (num % i == 0) 
            num /= i;
    }
    if (num > 1) 
        factors.push_back(num);
    return factors;
}

static bool different_vector(vector<int> &v1, vector<int> &v2) {
    if (v1.size() != 4)
        return false;
    if (v2.size() != 4)
        return false;
    for (auto e1 : v1) {
        for (auto e2 : v2) {
            if (e1 == e2)
                return false;
        }
    }
    return true;
}

static void vector_display(const vector<int> &v) {
    for (auto e : v) {
        cout << e << " ";
    }
    cout << endl;
}

int main() {
    int a = 210;
    auto factors_a = prime_factorization(a);
    auto factors_b = prime_factorization(a + 1);
    auto factors_c = prime_factorization(a + 2);
    auto factors_d = prime_factorization(a + 3);
    while (true) {
        bool b12 = different_vector(factors_a, factors_b);
        bool b23 = different_vector(factors_b, factors_c);
        bool b34 = different_vector(factors_c, factors_d);
        if (b12 && b23 && b34) {
            vector_display(factors_a);
            vector_display(factors_b);
            vector_display(factors_c);
            vector_display(factors_d);
            cout << a << endl;
            break;
        } else if (!b34) {
            a += 3;
            factors_a = factors_d;
            factors_b = prime_factorization(a + 1);
            factors_c = prime_factorization(a + 2);
            factors_d = prime_factorization(a + 3);
        } else if (!b23) {
            a += 2;
            factors_a = factors_c;
            factors_b = factors_d;
            factors_c = prime_factorization(a + 2);
            factors_d = prime_factorization(a + 3);
        } else if (!b12) {
            a += 1;
            factors_a = factors_b;
            factors_b = factors_c;
            factors_c = factors_d;
            factors_d = prime_factorization(a + 3);
        }
    }
}